package com.wolfcode.customerlinkmane.service.impl;

import com.wolfcode.common.utils.DateUtils;
import com.wolfcode.common.utils.PhoneUtil;
import com.wolfcode.common.utils.SecurityUtils;
import com.wolfcode.customerlinkmane.domain.CustomerLinkmane;
import com.wolfcode.customerlinkmane.domain.vo.CustomerLinkmaneInfo;
import com.wolfcode.customerlinkmane.domain.vo.CustomerLinkmaneUpdateVo;
import com.wolfcode.customerlinkmane.domain.vo.CustomerLinkmaneVo;
import com.wolfcode.customerlinkmane.enums.ActiveStatus;
import com.wolfcode.customerlinkmane.mapper.CustomerLinkmaneMapper;
import com.wolfcode.customerlinkmane.service.ICustomerLinkmaneService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 客户联系人Service业务层处理
 *
 * @author wolfcode
 * @date 2022-12-09
 */
@Service
public class CustomerLinkmaneServiceImpl implements ICustomerLinkmaneService {


    @Autowired
    private CustomerLinkmaneMapper customerLinkmaneMapper;

    /**
     * 查询客户联系人
     *
     * @param id 客户联系人主键
     * @return 客户联系人
     */
    @Override
    public CustomerLinkmane selectCustomerLinkmaneById(Long id)
    {
        return customerLinkmaneMapper.selectCustomerLinkmaneById(id);
    }

    /**
     * 查询客户联系人列表
     *
     * @param customerLinkmane 客户联系人
     * @return 客户联系人
     */
    @Override
    public List<CustomerLinkmaneInfo> selectCustomerLinkmaneList(CustomerLinkmane customerLinkmane)
    {
        List<CustomerLinkmaneInfo> customerLinkmaneInfos = customerLinkmaneMapper.selectCustomerLinkmaneListAll(customerLinkmane);
        return customerLinkmaneInfos;
    }

    /**
     * 新增客户联系人
     *
     * @param linkmaneVo 客户联系人
     * @return 结果
     */
    @Override
    public int insertCustomerLinkmane(CustomerLinkmaneVo linkmaneVo) {
        // 参数校验
        Assert.notNull(linkmaneVo, "非法参数");
        Assert.notNull(linkmaneVo.getCustomerId(), "客户不能为空");
        Assert.notNull(linkmaneVo.getLinkman(), "联系人名称不能为空");
        Assert.notNull(linkmaneVo.getPhone(), "手机号不能为空");
        Assert.notNull(linkmaneVo.getGender(), "性别不能为空");
        Assert.notNull(linkmaneVo.getAge(), "年龄不能为空");
        Assert.state(linkmaneVo.getAge() < 100, "年龄不能大于100");
        Assert.state(linkmaneVo.getDepartment().length() < 20, "20字以内");
        Assert.state(linkmaneVo.getPosition().length() < 20, "20字以内");
        Assert.state(PhoneUtil.isMobileNumber(linkmaneVo.getPhone()),"手机号不合法");
        CustomerLinkmane customerLinkmane = new CustomerLinkmane();
        BeanUtils.copyProperties(linkmaneVo, customerLinkmane);
        // 当前员工用户
        Long userId = SecurityUtils.getLoginUser().getUserId();
        customerLinkmane.setInputUser(userId);
        System.out.println(customerLinkmane.getCustomerId());
        // 当前录入时间
        customerLinkmane.setInputTime(DateUtils.getNowDate());
        // 新增客户联系人默认在职状态
        customerLinkmane.setPositionState(ActiveStatus.ZERO.ordinal());
        return customerLinkmaneMapper.insertCustomerLinkmane(customerLinkmane);
    }

    /**
     * 修改客户联系人
     *
     * @param linkmaneVo 客户联系人
     * @return 结果
     */
    @Override
    public void updateCustomerLinkmane(CustomerLinkmaneUpdateVo linkmaneVo) {
        // 参数校验
        Assert.notNull(linkmaneVo, "非法参数");
        Assert.notNull(linkmaneVo.getId(), "非法参数");
        Assert.notNull(linkmaneVo.getCustomerId(), "客户不能为空");
        Assert.notNull(linkmaneVo.getLinkman(), "联系人名称不能为空");
        Assert.notNull(linkmaneVo.getPhone(), "手机号不能为空");
        Assert.notNull(linkmaneVo.getGender(), "性别不能为空");
        Assert.notNull(linkmaneVo.getAge(), "年龄不能为空");
        Assert.state(linkmaneVo.getAge() < 100, "年龄不能大于100");
        Assert.state(linkmaneVo.getDepartment().length() < 20, "20字以内");
        Assert.state(linkmaneVo.getPosition().length() < 20, "20字以内");
        Assert.state(PhoneUtil.isMobileNumber(linkmaneVo.getPhone()),"手机号不合法");
        CustomerLinkmane customerLinkmane = new CustomerLinkmane();
        BeanUtils.copyProperties(linkmaneVo,customerLinkmane);
        customerLinkmaneMapper.updateCustomerLinkmane(customerLinkmane);
    }


    /**
     * 批量删除客户联系人
     *
     * @param ids 需要删除的客户联系人主键
     * @return 结果
     */
    @Override
    public int deleteCustomerLinkmaneByIds(Long[] ids)
    {
        Assert.notNull(ids,"非法参数");
        for (Long id : ids) {
            Assert.notNull(id,"非法参数");
            CustomerLinkmane customerLinkmane = customerLinkmaneMapper.selectCustomerLinkmaneById(id);
            Assert.notNull(customerLinkmane,"客户联系人不存在");
        }
        return customerLinkmaneMapper.deleteCustomerLinkmaneByIds(ids);
    }

    /**
     * 删除客户联系人信息
     *
     * @param id 客户联系人主键
     * @return 结果
     */
    @Override
    public int deleteCustomerLinkmaneById(Long id)
    {
        Assert.notNull(id,"非法参数");
        CustomerLinkmane customerLinkmane = customerLinkmaneMapper.selectCustomerLinkmaneById(id);
        Assert.notNull(customerLinkmane,"客户联系人不存在");
        return customerLinkmaneMapper.deleteCustomerLinkmaneById(id);
    }

    @Override
    public List<CustomerLinkmane> selectCustomerLinkmaneListAll(Long customerId) {
        Assert.notNull(customerId,"非法参数");
        CustomerLinkmane customerLinkmane = new CustomerLinkmane();
        customerLinkmane.setCustomerId(customerId);
        List<CustomerLinkmane> customerLinkmanes = customerLinkmaneMapper.selectCustomerLinkmaneList(customerLinkmane);
        return customerLinkmanes;
    }
}

